﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Mobile.Mvc;
using System.Windows.Forms;

namespace TouchUI.System.Mobile.Mvc
{
  /// <summary>
  /// A TouchUI dialog implementing the System.Mobile.Mvc.IView interface
  /// </summary>
  public class ViewDialog : Dialog, IView
  {
      #region fields
      private UserControl invoke = null;
      private ViewDataDictionary _viewData;

      #endregion

      #region constructors

      /// <summary>
      /// Initializes a new instance of the ViewForm class.
      /// </summary>
      public ViewDialog()
      {
        invoke = new UserControl();
      }

      #endregion

      #region methods

      // Raises the ViewStateChanged event
      protected void OnViewStateChanged(string key)
      {
          if (this.ViewStateChanged != null)
          {
              this.ViewStateChanged(key);
          }
      }

      protected virtual void OnUpdateView(string key)
      {

      }       

      private void SetViewData(ViewDataDictionary viewData)
      {
          this._viewData = viewData;
      }

      
      /// <summary>
      /// Used to notify the view of the updated model data.
      /// </summary>
      /// <param name="key">The key to indentify the data.</param>
      public void UpdateView(string key)
      {
          if (!invoke.InvokeRequired)
          {
              OnUpdateView(key);
          }
          else
          {
            invoke.Invoke(new Action<string>(OnUpdateView), key);
          }
      }      

      #endregion

      #region IView Members

      /// <summary>
      /// The state change event of the view
      /// </summary>
      public event StateChangedEventHandler ViewStateChanged;

      /// <summary>
      /// Gets or sets the ViewData. Supports assigning the ViewData from a different thread.
      /// </summary>
      public ViewDataDictionary ViewData
      {
          get
          {
              if (_viewData == null)
              {
                  _viewData = new ViewDataDictionary();
              }
              return _viewData;
          }
          set
          {
              if (!invoke.InvokeRequired)
              {
                  _viewData = value;
              }
              else
              {
                invoke.Invoke(new Action<ViewDataDictionary>(SetViewData), value);
              }
          }
      }
      
      /// <summary>
      /// Gets or sets the Model.
      /// </summary>
      public object Model
      {
          get
          {
              return _viewData.Model;
          }
          set
          {
              _viewData.Model = value;
          }
      }

      public void Show()
      {
        NavigatorService.TopMost = this;
        Paint(Common.Instance.PaintGraphics);        
      }

      public void Close()
      {
        //throw new NotImplementedException();
      }

      public void Hide()
      {
        //throw new NotImplementedException();
      }

      public DialogResult ShowDialog()
      {
        Paint(Common.Instance.PaintGraphics);
        return DialogResult.OK;
      }

      #endregion
  }
}
